package cn.iocoder.yudao.module.pi.controller.admin.pipline.deploy;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.pi.controller.admin.pipline.deploy.vo.*;
import cn.iocoder.yudao.module.pi.dal.dataobject.deploy.DeployDO;
import cn.iocoder.yudao.module.pi.framework.config.HuaWeiIAMPoperties;
import cn.iocoder.yudao.module.pi.service.deploy.DeployService;
import com.alibaba.fastjson2.JSON;
import com.huaweicloud.sdk.codeartspipeline.v2.CodeArtsPipelineClient;
import com.huaweicloud.sdk.codeartspipeline.v2.model.*;
import com.huaweicloud.sdk.codeartspipeline.v2.region.CodeArtsPipelineRegion;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;

import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 流水线部署")
@RestController
@RequestMapping("/pi/pipline/deploy")
@Slf4j
public class PiplineDeployController {
    @Resource
    private DeployService deployService;

    @PostMapping("/create")
    @Operation(summary = "创建部署")
    //@PreAuthorize("@ss.hasPermission('pi:deploy:create')")
    @PermitAll
    @TenantIgnore
    public CommonResult<Long> createDeploy(@Valid @RequestBody DeploySaveReqVO createReqVO) {
        createReqVO.setData(JSON.toJSONString(createReqVO.getTags()));
        return success(deployService.createDeploy(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新部署")
    //@PreAuthorize("@ss.hasPermission('pi:deploy:update')")
    @PermitAll
    @TenantIgnore
    public CommonResult<Boolean> updateDeploy(@Valid @RequestBody DeploySaveReqVO updateReqVO) {
        updateReqVO.setData(JSON.toJSONString(updateReqVO.getTags()));
        deployService.updateDeploy(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除部署")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('pi:deploy:delete')")
    @PermitAll
    @TenantIgnore
    public CommonResult<Boolean> deleteDeploy(@RequestParam("id") Long id) {
        deployService.deleteDeploy(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得部署")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('pi:deploy:query')")
    @PermitAll
    @TenantIgnore
    public CommonResult<DeployRespVO> getDeploy(@RequestParam("id") Long id) {
        DeployDO deploy = deployService.getDeploy(id);
        DeployRespVO deployRespVO = BeanUtils.toBean(deploy, DeployRespVO.class);
        deployRespVO.setTags(JSON.parseArray(deploy.getData(), DeployBaseVO.DataDTO.class));
        deployRespVO.setData(null);
        return success(deployRespVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得部署分页")
    //@PreAuthorize("@ss.hasPermission('pi:deploy:query')")
    @PermitAll
    @TenantIgnore
    public CommonResult<PageResult<DeployRespVO>> getDeployPage(@Valid DeployPageReqVO pageReqVO) {
        PageResult<DeployDO> pageResult = deployService.getDeployPage(pageReqVO);
        PageResult<DeployRespVO> pageResultVO =BeanUtils.toBean(pageResult, DeployRespVO.class);
        pageResultVO.getList().forEach(item->{
            item.setTags(JSON.parseArray(item.getData(), DeployBaseVO.DataDTO.class));
            item.setData(null);
        });
        return success(pageResultVO);
    }

}
